//
//  MCVerificationCodeTextField.swift
//  StoreManage
//
//  Created by MC on 2018/8/29.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit


/**
 输入框： 包括发送验证码按钮，以及倒计时的功能处理
 */

class MCVerificationCodeTextField: UIView {

    
    public var text = ""
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(1)
        }
        
        self.addSubview(cutDownButton)
        cutDownButton.snp.remakeConstraints { (make) ->Void in
            make.top.bottom.right.equalTo(self)
            make.width.equalTo(100)
        }
        
        self.addSubview(codeTextField)
        codeTextField.snp.remakeConstraints { (make) ->Void in
            make.left.top.bottom.equalTo(self)
            make.right.equalTo(cutDownButton.snp.left)
        }
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        codeTextField.text = text
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    lazy var codeTextField: MCTextFiled = {
        let tf = MCTextFiled()
        tf.isHiddenLine = true
        tf.keyBoardType = MCKeyBorardType.Number
        tf.placeholder = "请输入验证码"
        tf.delegate = self
        return tf
    }()
    
    lazy var cutDownButton: MCCutDownButton = {
        let button = MCCutDownButton()
        
        return button
    }()
}

extension MCVerificationCodeTextField : UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        text = newString
                
        return true
    }
}
